/**
 *    Copyright 2014 all reserved by Ihome corporate, any persons
 *    or corporate must not use this code for business purpose before
 *    permission is gained.
 *  
 *    ******                                      ******
 *    ******   I Love My Home       ******
 *    ******                                      ******
 *    
 *    Author: DMF
 *    Create Date: Mar 19, 2014
 *
 */
package com.ihome.servlet;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class DelegatingServletProxy extends GenericServlet {

	private String targetBean;
	private Servlet proxy;

	@Override
	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		proxy.service(request, response);

	}

	@Override
	public void init() throws ServletException {
		this.targetBean = getServletName();
		getServletBean();
		proxy.init(getServletConfig());
	}

	private void getServletBean() {
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		this.proxy = (Servlet) wac.getBean(targetBean);
	}

}
